package com.ad.pet.message;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ad.pet.DaggerMainActivityCompent;
import com.ad.pet.MainActivity;
import com.ad.pet.R;
import com.ad.pet.app.Navigation;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.app.main.HomeContentContract;
import com.ad.pet.app.main.HomeContentPresenter;
import com.ad.pet.common.data.utils.ui.ToastUtil;
import com.ad.pet.common.structure.events.RxBus;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.activity.AppToolbarActivity;
import com.ad.pet.message.adapter.MesGridAdapter;
import com.ad.pet.message.adapter.MessageViewAdapter;
import com.ad.pet.models.remote.home.Cate;
import com.ad.pet.models.remote.home.floor.MainFloor;
import com.ad.pet.models.remote.home.floor.PopNewRegist;
import com.ad.pet.models.remote.message.Message;
import com.ad.pet.utils.LyxUtils;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import rx.functions.Action1;

/**
 * Created by SEELE on 2019/6/20.
 */

public class MessageActivity extends AppToolbarActivity implements MessageContract.View, HomeContentContract.View{

    @Inject
    MessagePresenter mMessagePresenter;
    @BindView(R.id.view_point)
    View mViewPoint;
    @BindView(R.id.tv_unred_number)
    TextView mTvUnredNumber;
    @BindView(R.id.recycler_top)
    RecyclerView mRecyclerTop;
    @BindView(R.id.recycelr_bottom)
    RecyclerView mRecycelrBottom;
    private MessageViewAdapter mMessageViewAdapter;
    private MesGridAdapter mTopViewAdapter;

    @BindView(R.id.relativeLayout_msg)
    RelativeLayout mRelativeLayoutMsg;

    @BindView(R.id.iv_clean)
    ImageView ivClean;
    @Inject
    HomeContentPresenter mHomeContentPresenter;

    @Inject
    RxDisposable mRxDisposable;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mStatusLayoutManager.showContent();
        mToolbar.setTitle(getString(R.string.message));
    }

    @Override
    protected int contentLayout() {
        return R.layout.fragment_message;
    }

    @Override
    protected void clickRetry(View v) {

    }

    @Override
    protected void initViews() {
        mMessagePresenter.onCreate(this);
        mHomeContentPresenter.onCreate(this);
        mMessagePresenter.getMessage();
        mHomeContentPresenter.getMessagesNumber();

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        mRecycelrBottom.setLayoutManager(linearLayoutManager);
        mMessageViewAdapter = new MessageViewAdapter(null);
        mRecycelrBottom.setAdapter(mMessageViewAdapter);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(mActivity, 3);
        mRecyclerTop.setLayoutManager(gridLayoutManager);
        mTopViewAdapter = new MesGridAdapter(null);
        mRecyclerTop.setAdapter(mTopViewAdapter);


    }

    @Override
    protected void initData() {


    }

    @Override
    protected void initEvents() {

        mRelativeLayoutMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMessagePresenter.isTypeReadMessage("all");
            }
        });

        mTopViewAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                Message message = mTopViewAdapter.getData().get(position);
                switch (position){
                    case 0:
                        Navigation.goSystemMessage(mActivity,message.getCid(), MainActivity.REQUEST_GO_MALL);
                        break;
                    case 1:
                        Navigation.goOrderMessage(mActivity,message.getCid(), MainActivity.REQUEST_GO_MALL);
                        break;
                    case 2:
                        Navigation.goSaleMessage(mActivity,message.getCid(), MainActivity.REQUEST_GO_MALL);
                        break;
                }
            }
        });

        mMessageViewAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Message message = mMessageViewAdapter.getData().get(position);

                switch (position){
                    case 0:
                        Navigation.goSystemMessage(mActivity,message.getCid(), MainActivity.REQUEST_GO_MALL);
                        break;
                    case 1:
                        Navigation.goOrderMessage(mActivity,message.getCid(), MainActivity.REQUEST_GO_MALL);
                        break;
                    case 2:
                        Navigation.goSaleMessage(mActivity,message.getCid(), MainActivity.REQUEST_GO_MALL);
                        break;
                }
            }
        });

        mRxDisposable.add(RxBus.getDefault().toObservable(String.class)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        if ("set_message_number_success".equals(s)) {
                            mHomeContentPresenter.getMessagesNumber();
                            mMessagePresenter.getMessage();
                        }
                    }
                }));

    }

    @Override
    public void initInject() {
        DaggerMainActivityCompent.builder().applicationComponent(getApplicationComponent()).activityModule(getActivityModule()).build().inject(this);
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void error(int code, String msg) {

    }

    @Override
    public void getMessageSuccess(List<Message> messages) {


        messages.get(0).setImg(LyxUtils.getImgUrl(R.drawable.ic_sysmsg));
        messages.get(1).setImg(LyxUtils.getImgUrl(R.drawable.ic_ordermsg));
        messages.get(2).setImg(LyxUtils.getImgUrl(R.drawable.ic_youhuimsg));

        mMessageViewAdapter.setNewData(messages);

        List<Message> topMsgs = messages;

        mTopViewAdapter.setNewData(topMsgs);

    }

    @Override
    public void getTypeMessageSuccess(List<Message> messages) {

    }

    @Override
    public void deleteMessageSuccess() {

    }

    @Override
    public void isReadMessageSuccess() {

    }

    @Override
    public void isTypeReadMessageSuccess() {

        ToastUtil.getToastUtil().showShort(getString(R.string.clean_msg_success));
        mMessagePresenter.getMessage();
        mHomeContentPresenter.getMessagesNumber();

        RxBus.getDefault().post("set_message_number_success");
    }


    @Override
    public void getTopBarSuccess(List<Cate> cates) {

    }

    @Override
    public void getTopBarMenuSucces(List<Cate> cates) {

    }

    @Override
    public void getMessageNumberSuccess(int number) {
        if (number == 0) {
            ivClean.setVisibility(View.GONE);
            mViewPoint.setVisibility(View.GONE);
        } else {
            ivClean.setVisibility(View.VISIBLE);
            mViewPoint.setVisibility(View.VISIBLE);
        }

        mTvUnredNumber.setText(number + getString(R.string.unread_msg));
    }

    @Override
    public void getHomeFloorSuccess(List<MainFloor> mainFloor) {

    }

    @Override
    public void get_check_is_sign_homeSuccess(PopNewRegist popNewRegist) {

    }

    @Override
    public void register_sign_homeSuccess(PopNewRegist popNewRegist) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null)
            return;
        switch (requestCode) {

            case MainActivity.REQUEST_GO_MALL:
                setResult(RESULT_OK, data);
                finish();

                break;

        }
    }
}
